<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="68px"
          class="globalSerchFormStyle"
        >
          <el-row>
            <el-col :span="6">
              <el-form-item label="手机号码" prop="phoneNumber">
                <el-input
                  v-model="queryParams.phoneNumber"
                  placeholder="请输入手机号码"
                  clearable
                  size="small"
                  style="width: 190px"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="起始日期" prop="startingTime">
                <el-date-picker
                  size="small"
                  style="width: 190px"
                  v-model="queryParams.startingTime"
                  type="date"
                  placeholder="选择起始日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="结束日期" prop="endTimeint">
                <el-date-picker
                  size="small"
                  style="width: 190px"
                  v-model="queryParams.endTimeint"
                  type="date"
                  placeholder="选择结束日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
    <el-card shadow="always">
      <el-row style="text-align: center">
        <el-col :span="12">
          <div class="box br">
            <div class="title">收款订单数</div>
            <div class="num">{{ conditionCount.count }}个</div>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="box">
            <div class="title">收款总金额</div>
            <div class="num">{{ conditionCount.sum }}元</div>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-row :gutter="10" class="mtb16">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
        >
          搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          type="primary"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          type="primary"
          plain
          >新增</el-button
        >
      </el-col>
    </el-row>
    <template>
      <el-table
        :data="tableData"
        style="width: 100%"
        height="350"
        v-loading="loading"
        stripe
      >
        <el-table-column prop="id" label="ID" width="80"> </el-table-column>
        <el-table-column prop="cellPhoneNumber" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="amountCollected" label="收款金额">
        </el-table-column>
        <el-table-column
          prop="illustrate"
          label="描述/说明"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="paymentMethod"
          label="收款方式"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column prop="collectionTime" label="收款时间">
        </el-table-column>
        <el-table-column
          label="操作"
          align="left"
          fixed="right"
          prop="payDate"
          width="300px"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.row)"
              size="mini"
              style="margin: 0px 5px"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="handleDel(scope.row)"
              size="mini"
              style="margin: 0px 5px"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.pageSize" /> -->
      <el-pagination
        class="fr"
        style="margin-top: 8px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </template>
    <cashReceiptsForm
      :parm="params"
      @on-submit-add="hanleRefresh"
      @on-submit-edit="hanleRefresh"
    />
  </div>
</template>
<script>
import {
  queryByPage,
  cashReceiptsDeleteById,
} from "@/api/finance/cashReceipts";
import cashReceiptsForm from "./cashReceipts-form.vue";
import { cloneDeep } from "lodash";

export default {
  name: "cashReceipts",
  components: { cashReceiptsForm },
  data() {
    return {
      total: 0,
      // 查询参数
      queryParams: {
        phoneNumber: "",
        startingTime: "",
        endTimeint: "",
      },
      page: 1,
      pageSize: 10,
      loading: false,
      tableData: [],
      conditionCount: {
        count: 0,
        sum: 0,
      },
      params: {},
    };
  },
  methods: {
    // 新增
    handleAdd() {
      this.params = {
        drawerType: "new",
        drawerShow: true,
        editData: {
          paymentType: 0,
        },
      };
    },
    // 编辑
    handleEdit(row) {
      this.params = {
        drawerType: "edit",
        drawerShow: true,
        editData: cloneDeep(row),
      };
    },
    // 删除
    handleDel(row) {
      this.$confirm(`确认删除手机号为 ${row.cellPhoneNumber}  的收款吗？`).then(
        () => {
          cashReceiptsDeleteById([row.id]).then((res) => {
            if (res && res.code === 200) {
              this.$message.success(res.msg);
              this.getData();
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      );
    },
    handleQuery() {
      this.getData();
    },
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.getData();
    },
    getQueryData(data) {
      let queryData = data;
      Object.keys(queryData).forEach((key) => {
        if (queryData[key] === "" || queryData[key] === undefined) {
          queryData[key] = null;
        }
      });
      return queryData;
    },
    // 新增/编辑成功
    hanleRefresh() {
      this.getData();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getData();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getData();
    },
    // 判断账号角色
    authUser() {
      let roles = this.$store.state.user.roles;
      let data = this.getQueryData(this.queryParams);
      if (!roles.includes("admin")) {
        if (roles.includes("1")) {
          data.enterpriseId = this.presentEnterprise.enterpriseId;
        } else {
          data.stationId = this.presentSation;
        }
      }
      return data;
    },
    // 查询
    getData() {
      if (this.loading) return;
      this.loading = true;
      let queryData = this.authUser();
      queryData = Object.assign(
        {
          page: this.page,
          pageSize: this.pageSize,
        },
        queryData
      );
      queryByPage(queryData).then((res) => {
        if (res && res.code === 200) {
          this.tableData = res.data.pageInfo.list;
          this.total = res.data.count;
          this.conditionCount = res.data.conditionCount;
        } else {
          this.total = 0;
          this.tableData = [];
        }
      });
      this.loading = false;
    },
  },
  mounted() {
    this.getData();
  },
};
</script>
<style scoped lang="scss">
.box {
  display: flex;
  flex-direction: column;

  .title {
    flex: 1;
    font-size: 22px;
    color: rgb(103, 102, 102);
  }

  .num {
    flex: 1;
    font-size: 26px;
    font-weight: 400;
  }
}

.br {
  border-right: 1px dashed #ccc;
}

.mtb16 {
  margin: 16px 0;
}
</style>
